package com.knight.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.time.LocalDateTime;

/**
 * 自定义处理消息的Handler
 * TextWebSocketFrame：在Netty中，是用于为webSocket专门处理文本的对象，frame是消息的载体
 */
public class ChatHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    // 用于记录和管理所有客户端的channel
    private static ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {

        // 获取客户端传输过来的消息
        String content = msg.text();
        System.out.println("接收到的消息：" + content);

//        for (Channel channel: clients) {
//            channel.writeAndFlush(new TextWebSocketFrame(
//                    "[服务器在" + LocalDateTime.now() + "接收到消息, 消息为：" + content + "]"));
//        }
        // 与上面的for循环达到的效果一样
        clients.writeAndFlush(new TextWebSocketFrame(
                "[服务器在" + LocalDateTime.now() + "接收到消息, 消息为：" + content + "]"));
    }

    /**
     * 获取客户端的channel，并且放到channelGroup中进行管理
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        // 客户端创建完后 Handler就会触发
        clients.add(ctx.channel());
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端断开，channel对应的长id为：" + ctx.channel().id().asLongText());
        System.out.println("客户端断开，channel对应的短id为：" + ctx.channel().id().asShortText());
    }
}
